#include "qmenubutton.h"

QMenuButton::QMenuButton(QWidget * parent) : QPushButton(parent)
{
    status = NORMAL;
    mouse_press = false;
    icon_path = "/new/icon/";

    QMenuButton::setFixedSize(44,44);
    QMenuButton::setStyleSheet("border:none");

    buttonImage = new QLabel();
    buttonImage->setFixedSize(30,30);
    buttonImage->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");

    buttonTxt = new QLabel();
    buttonTxt->setFixedHeight(12);
    buttonTxt->setStyleSheet("color:white");
    QFont f;
    f.setPointSize(6);
    buttonTxt->setFont(f);
    buttonTxt->setAlignment(Qt::AlignHCenter|Qt::AlignTop);

    QVBoxLayout *topLayout = new QVBoxLayout();
    topLayout->addWidget(buttonImage);
    topLayout->setContentsMargins(7,0,0,0);

    QVBoxLayout *bottomLayout = new QVBoxLayout();
    bottomLayout->addWidget(buttonTxt);
    bottomLayout->setMargin(0);
    bottomLayout->setSpacing(0);

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);
    mainLayout->addLayout(topLayout);
    mainLayout->addLayout(bottomLayout);

    QMenuButton::setLayout(mainLayout);
}

void QMenuButton::setNormalImg(QString s) {
    normalImg = s;
    buttonImage->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
}

void QMenuButton::sethoverImg(QString s) {
    hoverImg = s;
}

void QMenuButton::setPressImg(QString s) {
    pressImg = s;
}

void QMenuButton::setButtonTxt(QString s) {
    buttonTxt->setText(s);
}

void QMenuButton::enterEvent(QEvent *)
{
//    buttonImage->setStyleSheet("QLabel{border-image:url(:" + hoverImg + ")}");
    setStyleSheet("background-color:#474747;");
    if (!needSelect) {
        status = HOVER;
        buttonTxt->setStyleSheet("color:#919191");
    }

    update();
}

void QMenuButton::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        mouse_press = true;
        if (needSelect) {
            if (status == PRESS) {
                buttonImage->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
                buttonTxt->setStyleSheet("color:white");
                status = NORMAL;
            }else {
                buttonImage->setStyleSheet("QLabel{border-image:url(:" + pressImg + ")}");
                buttonTxt->setStyleSheet("color:#ff4747");
                status = PRESS;
            }
        }else {
            buttonImage->setStyleSheet("QLabel{border-image:url(:" + pressImg + ")}");
            buttonTxt->setStyleSheet("color:#4381ff");
            status = PRESS;
        }
    }
}

void QMenuButton::mouseReleaseEvent(QMouseEvent *event)
{
    if(mouse_press&&this->rect().contains(event->pos()))
    {
        mouse_press = false;
        if (!needSelect) {
            status = HOVER;
            buttonImage->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
            buttonTxt->setStyleSheet("color:white");
        }

        emit clicked();
    }
}

void QMenuButton::leaveEvent(QEvent *)
{
    if (!needSelect) {
        status = NORMAL;
        buttonImage->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
        buttonTxt->setStyleSheet("color:white");
    }else {
        if (status != PRESS) {
            buttonImage->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
            setStyleSheet("background-color:#313131;");
            buttonTxt->setStyleSheet("color:white");
        }
    }

    setStyleSheet("background-color: rgb(28, 30, 32);");
}
